package com.wuseguang.springcloud.timer;

import com.wuseguang.springcloud.dao.jdbc.SaveAttendJdbc;
import com.wuseguang.springcloud.util.DateUtil;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SaveAttendTimer extends QuartzJobBean {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SaveAttendJdbc attendJdbc;

    @Override
    @Transactional
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        logger.info("执行了SaveAttendTimer2.0V 时间={}", DateUtil.time());

        String pram = jobExecutionContext.getTrigger().getKey().getGroup();

        String schoolId = pram.split("-")[0];//学校id

        String gettime = DateUtil.addDays(DateUtil.gettime(), -1);//昨天时间

        int weeksInt = DateUtil.getWeeksInt(gettime);

        String sql = "SELECT id,IF(shift_id!=0,shift_id,(SELECT shift_id FROM attend_group_shift WHERE attend_group_id=attend_group.id AND sort=? ) ) shift_id FROM attend_group WHERE school_id=?";

        List<Map<String, Object>> list = attendJdbc.queryListMap(sql, weeksInt, schoolId);//获取所有考勤组

        String delSql = "delete from class_attendlist  WHERE   attend_group_id=?  AND   DATE(attendtime)= DATE_SUB(CURDATE(),INTERVAL 1 DAY) ";

        String delTwoSql = "delete from class_attendlist  WHERE   attend_group_id=?  AND attendtype is null AND  DATE(attendtime)= DATE_SUB(CURDATE(),INTERVAL 1 DAY) ";

        //休息 缺勤
        String addSql = "INSERT INTO class_attendlist(userid,attend_group_id,shift_id,attendtype,school_id,attendtime,sort,opertime) " +
                "SELECT userid,attend_group_id,?,?,?,DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 1 DAY),'%Y-%m-%d'),0,DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 1 DAY),'%Y-%m-%d') FROM attend_group_user WHERE attend_group_id=? ";

        //进班缺卡
        String addTwoSql = "INSERT INTO class_attendlist(userid,attend_group_id,shift_id,attendtype,school_id,attendtime,sort,opertime) " +
                "SELECT a.userid,a.attend_group_id,?,?,?,?,?,DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 1 DAY),'%Y-%m-%d')  FROM attend_group_user a LEFT JOIN class_attendlist l " +
                "ON ( a.attend_group_id=l.attend_group_id AND a.userid=l.userid AND l.attendtime  BETWEEN ? AND ? ) WHERE a.attend_group_id=? AND l.id IS  NULL  GROUP BY a.userid ";

        //缺勤
        String qqSql = "SELECT GROUP_CONCAT(a.userid) string FROM attend_group_user a LEFT JOIN class_attendlist l ON ( a.attend_group_id=l.attend_group_id AND a.userid=l.userid AND l.attendtime  BETWEEN ? AND ? ) WHERE a.attend_group_id=? AND l.id IS  NULL ";


        String sqlShift = "SELECT  `type`, `starttime_one`, `endtime_one`, `starttime_two`, `endtime_two` FROM attend_shift WHERE id=?";

        //String sqlAttendList = "SELECT  a.userid,GROUP_CONCAT(l.attendtime ORDER BY l.attendtime) time,GROUP_CONCAT(l.id ORDER BY l.attendtime) id FROM attend_group_user a LEFT JOIN class_attendlist l ON ( a.attend_group_id=l.attend_group_id AND a.userid=l.userid AND    DATE(l.attendtime)= DATE_SUB(CURDATE(),INTERVAL 1 DAY) ) WHERE a.attend_group_id=?   GROUP BY a.userid  ";

        String updateSql = "UPDATE class_attendlist SET attendtype=?,shift_id=?,sort=? WHERE id IN (SELECT id FROM (SELECT MIN(l.id) id  FROM attend_group_user a LEFT JOIN class_attendlist l  " +
                "ON ( a.attend_group_id=l.attend_group_id AND a.userid=l.userid AND l.attendtime  BETWEEN ? AND ?  ) WHERE a.attend_group_id=?  AND l.id IS NOT NULL GROUP BY a.userid) t )";

        String updateTwoSql = "UPDATE class_attendlist SET attendtype=?,shift_id=?,sort=? WHERE id IN (SELECT id FROM (SELECT a.userid,MAX(l.id) id,GROUP_CONCAT(attendtype) attendtype  FROM attend_group_user a LEFT JOIN class_attendlist l  " +
                " ON ( a.attend_group_id=l.attend_group_id AND a.userid=l.userid AND l.attendtime> ? AND l.attendtime<? ) WHERE a.attend_group_id=? AND l.id IS NOT NULL  GROUP BY a.userid) t WHERE attendtype IS NULL ) ";


        // String userSql = "SELECT t.id string FROM attend_group_user u,student t WHERE  u.userid=t.id AND u.attend_group_id=?";

        String updateGroupSql = "UPDATE attend_group SET shift_id=0 WHERE id=?";

        if (list != null && list.size() > 0) {
            for (Map<String, Object> map : list) {//处理所有考勤组
                Object id = map.get("id");//考勤组id
                String shift_id = map.get("shift_id").toString();
                if ("-1".equals(shift_id) || "0".equals(shift_id)) {//考勤为休息
                    attendJdbc.updateByParameter(delSql, id);
                    attendJdbc.updateByParameter(addSql, 0, 7, schoolId, id);
                } else {//按班次时间处理当前的考勤数据
                    //List<String> userList = attendJdbc.queryListString(userSql, new Object[]{id});
                    Map<String, Object> timeMap = null;
                    try {
                        timeMap = attendJdbc.queryMap(sqlShift, shift_id);
                    } catch (Exception e) {
                        logger.info("executeInternal2.0-->erorr={}", e);
                    }
                    int type = (int) timeMap.get("type");//考勤类型:0 1天1次、1 1天2次
                    String starttimeOne = DateUtil.AddTime(gettime, timeMap.get("starttime_one") + "");
                    String endtimeOne = DateUtil.AddTime(gettime, timeMap.get("endtime_one") + "");
                    String starttimeTwo = DateUtil.AddTime(gettime, timeMap.get("starttime_two") + "");
                    String endtimeTwo = DateUtil.AddTime(gettime, timeMap.get("endtime_two") + "");

                    //考勤类型: 0请假 1进班正常 2进班迟到 3进班缺卡  4离班正常 5离班早退  6离班缺卡 7休息 8缺勤
                    if (type == 0) {
                        String time = "";
                        if (!StringUtils.isEmpty(endtimeOne) && !StringUtils.isEmpty(starttimeTwo)) {//设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                            long l = DateUtil.getcompTime(starttimeTwo, endtimeOne);
                            time = DateUtil.addtimeMinutes(endtimeOne, (int) l / 2);
                        } else {//未设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                            long l = DateUtil.getcompTime(endtimeTwo, starttimeOne);
                            time = DateUtil.addtimeMinutes(starttimeOne, (int) l / 2);
                        }
                        String userString = attendJdbc.queryString(qqSql, gettime, DateUtil.gettime(), id);

                        attendJdbc.updateByParameter(updateSql, 1, shift_id, 0, gettime, starttimeOne, id);//进班正常
                        //判断是 进班迟到
                        attendJdbc.updateByParameter(updateTwoSql, 2, shift_id, 0, gettime, time, id);
                        attendJdbc.updateByParameter(addTwoSql, shift_id, 3, schoolId, starttimeOne, 0, gettime, time, id);//进班缺卡


                        attendJdbc.updateByParameter(updateSql, 5, shift_id, 1, time, endtimeTwo, id);//离班早退
                        //判断是 离班正常
                        attendJdbc.updateByParameter(updateTwoSql, 4, shift_id, 1, time, DateUtil.gettime(), id);
                        attendJdbc.updateByParameter(addTwoSql, shift_id, 6, schoolId, endtimeTwo, 1, time, DateUtil.gettime(), id);//进班缺卡


                        if (!StringUtils.isEmpty(userString)) {//缺勤
                            String qqAddSql = "INSERT INTO class_attendlist(userid,attend_group_id,shift_id,attendtype,school_id,attendtime,sort,opertime) " +
                                    "   SELECT a.id,?,?,8,?,DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 1 DAY),'%Y-%m-%d'),0,DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 1 DAY),'%Y-%m-%d')  FROM student  a WHERE a.id in (" + userString + ")";
                            attendJdbc.updateByParameter(qqAddSql, id, shift_id, schoolId);
                            String delAttendSql = "DELETE FROM  class_attendlist WHERE attend_group_id=?   AND userid IN  (" + userString + ") AND (attendtype=3 or attendtype=6)";
                            attendJdbc.updateByParameter(delAttendSql, id);
                        }
                        attendJdbc.updateByParameter(delTwoSql, id);//删除null的
                        attendJdbc.updateByParameter(updateGroupSql, id);//修改考勤组
                    } else {
                        //求第一段考勤的中间时间  用来区分 进班迟到和离班早退
                        long l = DateUtil.getcompTime(endtimeOne, starttimeOne);
                        String s = DateUtil.addtimeMinutes(starttimeOne, (int) l / 2);
                        //求第一段考勤的结束时间和第二段开始时间的中间时间  用来区分 离班正常和第二段的进班正常
                        long l1 = DateUtil.getcompTime(starttimeTwo, endtimeOne);
                        String s1 = DateUtil.addtimeMinutes(endtimeOne, (int) l1 / 2);
                        //求第二段考勤的中间时间  用来区分 进班迟到和离班早退
                        long l2 = DateUtil.getcompTime(endtimeTwo, starttimeTwo);
                        String s3 = DateUtil.addtimeMinutes(starttimeTwo, (int) l2 / 2);

                        String userString = attendJdbc.queryString(qqSql, gettime, DateUtil.gettime(), id);

                        attendJdbc.updateByParameter(updateSql, 1, shift_id, 0, gettime, starttimeOne, id);//进班正常
                        //判断是 进班迟到
                        attendJdbc.updateByParameter(updateTwoSql, 2, shift_id, 0, gettime, s, id);
                        attendJdbc.updateByParameter(addTwoSql, shift_id, 3, schoolId, starttimeOne, 0, gettime, s, id);//进班缺卡


                        attendJdbc.updateByParameter(updateSql, 5, shift_id, 1, s, endtimeOne, id);//离班早退
                        //判断是 离班正常
                        attendJdbc.updateByParameter(updateTwoSql, 4, shift_id, 1, s, s1, id);
                        attendJdbc.updateByParameter(addTwoSql, shift_id, 6, schoolId, endtimeOne, 1, s, s1, id);//离班缺卡


                        //考勤类型: 0请假 1进班正常 2进班迟到 3进班缺卡  4离班正常 5离班早退  6离班缺卡 7休息 8缺勤
                        //第二段
                        attendJdbc.updateByParameter(updateSql, 1, shift_id, 2, s1, starttimeTwo, id);//进班正常
                        //判断是 进班迟到
                        attendJdbc.updateByParameter(updateTwoSql, 2, shift_id, 2, s1, s3, id);
                        attendJdbc.updateByParameter(addTwoSql, shift_id, 3, schoolId, starttimeTwo, 2, s1, s3, id);//进班缺卡


                        //第二段
                        attendJdbc.updateByParameter(updateSql, 5, shift_id, 3, s3, endtimeTwo, id);//离班早退
                        //判断是 离班正常
                        attendJdbc.updateByParameter(updateTwoSql, 4, shift_id, 3, s3, DateUtil.gettime(), id);
                        attendJdbc.updateByParameter(addTwoSql, shift_id, 6, schoolId, endtimeTwo, 3, s3, DateUtil.gettime(), id);//离班缺卡

                        if (!StringUtils.isEmpty(userString)) {//缺勤
                            String qqAddSql = "INSERT INTO class_attendlist(userid,attend_group_id,shift_id,attendtype,school_id,attendtime,sort,opertime) " +
                                    "   SELECT a.id,?,?,8,?,DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 1 DAY),'%Y-%m-%d'),0,DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 1 DAY),'%Y-%m-%d')  FROM student  a WHERE a.id in (" + userString + ")";
                            attendJdbc.updateByParameter(qqAddSql, id, shift_id, schoolId);
                            String delAttendSql = "DELETE FROM  class_attendlist WHERE attend_group_id=?   AND userid IN  (" + userString + ") AND  attendtype=6";
                            attendJdbc.updateByParameter(delAttendSql, id);
                            delAttendSql = "DELETE FROM  class_attendlist WHERE attend_group_id=?   AND userid IN  (" + userString + ") AND attendtype=3 ";
                            attendJdbc.updateByParameter(delAttendSql, id);
                        }

                        attendJdbc.updateByParameter(delTwoSql, id);//删除null的
                        attendJdbc.updateByParameter(updateGroupSql, id);//修改考勤组
                    }
                }
            }
        }
    }





   /* private static String qingjia = "请假";

    private static String xiuxi = "休息";

    private static String queqin = "缺勤";*/

    /*@Override
    @Transactional
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        logger.info("执行了SaveAttendTimer时间={}", DateUtil.time());

        String pram = jobExecutionContext.getTrigger().getKey().getGroup();

        String schoolId = pram.split("-")[0];

        String sql = "SELECT id,shift_id,attend_group_type,opertime FROM attendance_group WHERE school_id=?";

        List<Map<String, Object>> list = attendJdbc.queryListMap(sql, schoolId);//获取所有考勤组

        if (list != null && list.size() > 0) {
            for (Map<String, Object> map : list) {//处理所有考勤组
                String shiftid = (String) map.get("shift_id");//班次id列
                String opertime = (String) map.get("opertime");
                int id = (int) map.get("id");
                String sql3 = " SELECT o.tid,o.NAME,p.cname FROM " +
                        "(SELECT t.id tid,t.student_name NAME,t.class_id  FROM attendance_group_user u,student t " +
                        " WHERE u.attend_group_id=? AND t.id=u.userid) o LEFT JOIN (SELECT a.name cname,a.id FROM grade g, classes a WHERE g.id=a.grade_id " +
                        " AND g.school_id=a.school_id AND a.school_id=? ) p ON o.class_id=p.id ";

                List<Map<String, Object>> userList = attendJdbc.queryListMap(sql3, id, schoolId);

                String sql2 = "SELECT id,TYPE,starttime,endtime,starttime1,endtime1 FROM attendance_shift WHERE school_id=" + schoolId;
                Map<String, Map<String, String>> mapShift = attendJdbc.getMapShift(sql2);//获取所有班次
                updateattendList(shiftid, userList, mapShift, opertime, DateUtil.gettime(), id + "", schoolId, 0, opertime);
            }
            sql = "update `attendance_group` set `opertime`=now() where school_id=?";
            attendJdbc.updateByParameter(sql, schoolId);
        }
    }


    @Transactional
    public String updateattendList(String shiftid, List<Map<String, Object>> userList, Map<String, Map<String, String>> mapShift, String time, String endtime, String attend_groupid, String school_id, int state, String starttime) {
        String sql = "insert into `attend_his` (`userid`, `attend_group_id`, `attendname`, `attendtype`, `school_id`, `opertime`,`attendtime`,`shift_id`) values(?,?,?,?,?,?,?,?)";
        if (userList == null || StringUtils.isEmpty(shiftid) || mapShift == null) {
            return null;
        }
        String[] split = shiftid.split(",");//格式 [1,2,3,4,5,0,0]
        long day = DateUtil.getDay(time, endtime);
        for (int i = 0; i < userList.size(); i++) {
            Map<String, Object> stringObjectMap = userList.get(i);
            int tid = (int) stringObjectMap.get("tid");   //教师id 或 学生id
            //考勤类型:
            //0 病假 1 事假 2 第一段：进班正常 3 第一段：进班迟到 4 第一段：进班缺卡 5 第一段：离班早退 6 第一段：离班正常 7 第一段：离班缺卡
            //8 第二段：进班正常 9 第二段：进班迟到 10 第二段：进班缺卡 11 第二段：离班早退 12 第二段：离班正常 13 第二段：离班缺卡 14 缺勤 15 休息
            for (int j = 0; j < day; j++) {
                String s2 = DateUtil.addDays(time, j);
                logger.info("updateattendList,考勤天数时间 attendtime={}", s2);
                int weekInt = DateUtil.getWeekInt(s2);
                int i1 = Integer.parseInt(split[weekInt - 1]);
                String sql2 = "SELECT  COUNT(1) count FROM  vacation WHERE student_id=? AND state=1 AND DATE(stime)<=DATE(?) AND DATE(etime)>= DATE(?)";
                if (attendJdbc.queryCount(sql2, tid, s2, s2) > 0) {
                    attendJdbc.updateByParameter(sql, tid, attend_groupid, qingjia, 0, school_id, s2, "", i1);
                    continue;
                }
                if (i1 == 0) {//为0 时 休息
                    attendJdbc.updateByParameter(sql, tid, attend_groupid, xiuxi, 15, school_id, s2, "", i1);
                } else {
                    logger.info("updateattendList,mapShift,i1={}", i1);
                    Map<String, String> stringStringMap = mapShift.get(String.valueOf(i1));
                    List<Map<String, Object>> attendList = null;//查询对应考勤组里面的所有打卡时间
                    String sql1 = "SELECT o.userid tid,o.attend_group_id,o.shift_id,o.attendtime,o.attendtype FROM attend o WHERE o.school_id=? AND o.userid=? " +
                            " AND o.attend_group_id=? AND o.state=0 AND TO_DAYS(o.attendtime)=TO_DAYS(?) ORDER BY attendtime ";
                    try {
                        attendList = attendJdbc.queryAttendListMap(sql1, school_id, tid, attend_groupid, s2);
                    } catch (Exception ex) {
                        logger.info("updateattendList-->updateattendList,error={}", ex);
                    }
                    logger.info("AttendServiceImpl==>updateattendList,sql1={}" + sql1);
                    updateAttendString(attendList, stringStringMap, s2, attend_groupid, tid, school_id, sql, i1);
                }
            }
        }
        return null;
    }

    @Transactional
    public int updateAttendString(List<Map<String, Object>> attendList, Map<String, String> stringStringMap, String s2, String attend_groupid, int tid, String school_id, String sql, int i1) {
        String getdaytime = DateUtil.getdaytime(s2);
        String type = stringStringMap.get("type");//考勤类型:0 1天1次、1 1天2次
        String starttime = stringStringMap.get("starttime");
        starttime = DateUtil.AddTime(getdaytime, starttime);
        String endtime = stringStringMap.get("endtime");
        endtime = DateUtil.AddTime(getdaytime, endtime);
        String starttime1 = stringStringMap.get("starttime1");
        starttime1 = DateUtil.AddTime(getdaytime, starttime1);
        String endtime1 = stringStringMap.get("endtime1");
        endtime1 = DateUtil.AddTime(getdaytime, endtime1);
        logger.info("attendString==>starttime={},endtime={},starttime1={},endtime1={}", starttime, endtime, starttime1, endtime1);
        int a = 0;//"进班缺卡"
        int b = 0;//"进班迟到"
        int c = 0;//"离班缺卡"
        int d = 0;//"离班早退"
        int e = 0;//"进班正常"
        int f = 0;//"离班正常"
        int g = 0;//"缺勤"

        //第二段
        int A = 0;//"进班缺卡"
        int B = 0;//"进班迟到"
        int C = 0;//"离班缺卡"
        int D = 0;//"离班早退"
        int E = 0;//"进班正常
        int F = 0;//"离班正常
        String time = "";
        if (attendList == null || attendList.size() <= 0) {//如果今天没有考勤视为 缺勤
            String s = "";
            if ("0".equals(type)) {
                s = "'',''";
            } else {
                s = "'','','',''";
            }
            attendJdbc.updateByParameter(sql, tid, attend_groupid, queqin, 14, school_id, s2, s, i1);
        } else {
            String time1 = "";
            String time2 = "";
            String time3 = "";
            if ("0".equals(type)) {//一天一段考勤
                String s = "";
                if (!StringUtils.isEmpty(endtime) && !StringUtils.isEmpty(starttime1)) {//设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                    long l = DateUtil.getcompTime(starttime1, endtime);
                    s = DateUtil.addtimeMinutes(endtime, (int) l / 2);
                } else {//未设置了中间休息时段  取中间时间 区分进班迟到 和 离班早退
                    long l = DateUtil.getcompTime(endtime1, starttime);
                    s = DateUtil.addtimeMinutes(starttime, (int) l / 2);
                }
                logger.info("attendString,s=={}", s);
                for (int i = 0; i < attendList.size(); i++) {//遍历这一天的考勤 进行判断
                    Map<String, Object> stringObjectMap = attendList.get(i);
                    String attendtime = (String) stringObjectMap.get("attendtime");
                    if (DateUtil.valueCompTime(starttime, attendtime) >= 0) {
                        if (b == 0) {
                            e = 1;//"进班正常"
                            time = attendtime;
                        }
                    } else {
                        if (DateUtil.valueCompTime(s, attendtime) >= 0) {//"进班迟到"
                            if (e == 0) {
                                b = 1;
                                time = attendtime;
                            }
                        }
                    }

                    if (DateUtil.valueCompTime(s, attendtime) < 0 && DateUtil.valueCompTime(endtime1, attendtime) > 0) {//"离班早退"
                        if (f == 0) {
                            d = 1;
                            time1 += "," + attendtime;
                        }
                    } else if (DateUtil.valueCompTime(endtime1, attendtime) <= 0) {//"离班正常"
                        if (d == 0) {
                            f = 1;
                            time1 += "," + attendtime;
                        }
                    }
                }
                if (e == 0 && b == 0) {
                    a = 1;//"进班缺卡"
                    time += "''";
                }
                if (d == 0 && f == 0) {
                    c = 1;//"离班缺卡"
                    time += ",''";
                }
                time += time1;
            } else {//一天两段考勤
                //求第一段考勤的中间时间  用来区分 进班迟到和离班早退
                long l = DateUtil.getcompTime(endtime, starttime);
                String s = DateUtil.addtimeMinutes(starttime, (int) l / 2);
                //求第一段考勤的结束时间和第二段开始时间的中间时间  用来区分 离班正常和第二段的进班正常
                long l1 = DateUtil.getcompTime(starttime1, endtime);
                String s1 = DateUtil.addtimeMinutes(endtime, (int) l1 / 2);
                //求第二段考勤的中间时间  用来区分 进班迟到和离班早退
                long l2 = DateUtil.getcompTime(endtime1, starttime1);
                String s3 = DateUtil.addtimeMinutes(starttime1, (int) l2 / 2);
                for (int i = 0; i < attendList.size(); i++) {//遍历这一天的考勤 进行判断
                    Map<String, Object> stringObjectMap = attendList.get(i);
                    String attendtime = (String) stringObjectMap.get("attendtime");
                    //第一段进班
                    if (DateUtil.valueCompTime(starttime, attendtime) >= 0) {
                        e = 1;//"进班正常"
                        time = attendtime;
                    } else {
                        if (DateUtil.valueCompTime(s, attendtime) >= 0) {//"进班迟到"
                            if (e == 0) {
                                b = 1;
                                time = attendtime;
                            }
                        }
                    }

                    //第一段离班
                    if (DateUtil.valueCompTime(endtime, attendtime) > 0 && DateUtil.valueCompTime(s, attendtime) < 0) {//"离班早退"
                        if (f == 0) {
                            d = 1;
                            time1 += "," + attendtime;
                        }
                    } else {
                        if (DateUtil.valueCompTime(s1, attendtime) >= 0 && DateUtil.valueCompTime(endtime, attendtime) <= 0) {//"离班正常
                            if (d == 0) {
                                f = 1;
                                time1 += "," + attendtime;
                            }
                        }
                    }

                    //第二段进班
                    if (DateUtil.valueCompTime(starttime1, attendtime) >= 0 && DateUtil.valueCompTime(s1, attendtime) < 0) {
                        E = 1;//"进班正常"
                        time2 = "," + attendtime;
                    } else {
                        if (DateUtil.valueCompTime(starttime1, attendtime) < 0 && DateUtil.valueCompTime(s3, attendtime) >= 0) {//"进班迟到"
                            if (E == 0) {
                                B = 1;
                                time2 = "," + attendtime;
                            }
                        }
                    }

                    //第二段离班
                    if (DateUtil.valueCompTime(endtime1, attendtime) > 0 && DateUtil.valueCompTime(s3, attendtime) < 0) {//"离班早退"
                        D = 1;
                        time3 = "," + attendtime;
                    } else {
                        if (DateUtil.valueCompTime(endtime1, attendtime) <= 0) {//"离班正常
                            if (D == 0) {
                                F = 1;
                                time3 = "," + attendtime;
                            }
                        }
                    }
                }
                if (e == 0 && b == 0) {
                    time = "''";
                    a = 1;//"进班缺卡"
                }

                if (d == 0 && f == 0) {
                    c = 1;//"离班缺卡"
                    time1 = ",''";
                }

                if (E == 0 && B == 0) {
                    A = 1;//"进班缺卡"
                    time2 = ",''";
                }
                if (D == 0 && F == 0) {
                    C = 1;//"离班缺卡"
                    time3 = ",''";
                }
                time += time1 + time2 + time3;
            }
        }
        if ("0".equals(type)) {
            if (c == 1 && a == 1) {
                g++;
            }
        } else {
            if (c == 1 && a == 1 && C == 1 && A == 1) {
                g++;
            }
        }
        String s1 = attendString(a, b, c, d, e, f, g, A, B, C, D, E, F, s2);
        //考勤类型:
        //0 病假 1 事假 2 第一段：进班正常 3 第一段：进班迟到 4 第一段：进班缺卡 5 第一段：离班早退 6 第一段：离班正常 7 第一段：离班缺卡
        //8 第二段：进班正常 9 第二段：进班迟到 10 第二段：进班缺卡 11 第二段：离班早退 12 第二段：离班正常 13 第二段：离班缺卡 14 缺勤 15 休息
        String s = attendString1(a, b, c, d, e, f, g, A, B, C, D, E, F);
        if (s.indexOf(",") != -1) {
            s = s.substring(0, s.length() - 1);
        }
        logger.info("updateAttendString,updateByParameter={}", a, b, c, d, e, f, g, A, B, C, D, E, F);
        if (!"".equals("" + tid) && s1 != null && !"".equals(s1)) {
            attendJdbc.updateByParameter(sql, tid, attend_groupid, s1, s, school_id, s2, time, i1);
        }
        return 0;
    }


    public String attendString(int a, int b, int c, int d, int e, int f, int g, int A, int B, int C, int D, int E, int F, String s2) {
        logger.info("attendString-->,a={},b={},c={},d={},e={},f={},g={},A={},B={},C={},D={},E={},F={}", a, b, c, d, e, f, g, A, B, C, D, E, F);
        StringBuilder builder = new StringBuilder();
        if (g == 1) {
            builder.append("缺勤");
            return builder.toString();
        }
        if (e == 1) {
            builder.append("进班正常 ");
        }
        if (a == 1) {
            builder.append("进班缺卡 ");
        }
        if (b == 1) {
            builder.append("进班迟到 ");
        }
        if (f == 1) {
            builder.append("离班正常");
        }
        if (c == 1) {
            builder.append("离班缺卡");
        }
        if (d == 1) {
            builder.append("离班早退");
        }

        if (A == 1) {
            builder.append(" 进班缺卡");
        }
        if (B == 1) {
            builder.append(" 进班迟到");
        }
        if (E == 1) {
            builder.append(" 进班正常");
        }
        if (C == 1) {
            builder.append(" 离班缺卡");
        }
        if (D == 1) {
            builder.append(" 离班早退");
        }
        if (F == 1) {
            builder.append(" 离班正常");
        }
        return builder.toString();
    }


    //0 病假 1 事假 2 第一段：进班正常 3 第一段：进班迟到 4 第一段：进班缺卡 5 第一段：离班早退 6 第一段：离班正常 7 第一段：离班缺卡
    //8 第二段：进班正常 9 第二段：进班迟到 10 第二段：进班缺卡 11 第二段：离班早退 12 第二段：离班正常 13 第二段：离班缺卡 14 缺勤 15 休息
    public String attendString1(int a, int b, int c, int d, int e, int f, int g, int A, int B, int C, int D, int E, int F) {
        String s = "";
        if (g == 1) {
            s += "14";
            return s;
        }
        if (e == 1) {
            s += "2,";
        }

        if (b == 1) {
            s += "3,";
        }
        if (a == 1) {
            s += "4,";
        }
        if (d == 1) {
            s += "5,";
        }
        if (f == 1) {
            s += "6,";
        }

        if (c == 1) {
            s += "7,";
        }
        if (E == 1) {
            s += "8,";
        }

        if (B == 1) {
            s += "9,";
        }
        if (A == 1) {
            s += "10,";
        }

        if (D == 1) {
            s += "11,";
        }

        if (F == 1) {
            s += "12,";
        }
        if (C == 1) {
            s += "13,";
        }
        return s;
    }
*/
}
